

#import "HNurseAssessRecordViewController.h"
#import "HNurseAssessUnloadViewController.h"
#import "HNurseAssessDidLoadViewController.h"
#import "HQc_codeModel.h"
#import "ModelToJson.h"
#import "Model_Hr_Ns_Inspect_New.h"
#import "InitDbTableQuality.h"
#import "UploadPubQuality.h"
#import "HCommon.h"
#import "HNightQualiatyViewController.h"
#import <MBProgressHUD.h>
#import "InitDbTableHSatisfaction.h"
@interface HNurseAssessRecordViewController ()


@property (nonatomic,strong) NSDictionary *theDictMenuParam;


@property (nonatomic,strong) UITableView *theTableView;



@property (nonatomic, strong) UISegmentedControl *segment;
@property (nonatomic, strong) UIView *Rootviews;
@property (nonatomic, assign) NSInteger isContrler;
@property (nonatomic, strong) HNurseAssessUnloadViewController *HunUploadVC;
@property (nonatomic, strong) HNurseAssessUnloadViewController *HDidUploadVC;
@property (nonatomic, copy) NSString *level_id;
@property (nonatomic, strong) NSDictionary *param;
@property (nonatomic, assign) NSInteger isHnight;
@property (nonatomic, assign) NSInteger menu_id;




@end

@implementation HNurseAssessRecordViewController


-(instancetype)initWithMenuParam:(NSDictionary *)dictMenuParam{
    self.theDictMenuParam = dictMenuParam;
    
    /**
     *  数据格式 Dictionary[ "A",ARRAY_UNUPLOAD,"B",ARRAY_UPLOADED]
     *
     */
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem *execbtn = [[UIBarButtonItem alloc] initWithTitle:@"上传" style:UIBarButtonItemStylePlain target:self action:@selector(didUploadButttonClicked:)];
    self.navigationItem.rightBarButtonItem = execbtn;
    
    [self createSegmentController];
    [self.view addSubview:self.Rootviews];
//    [self.view addSubview:self.theTableView];
}

- (void)createSegmentController  {
    self.segment = [[UISegmentedControl alloc]initWithItems:@[@"未上传",@"已上传"]];
    self.segment.tintColor = RGB(69, 185, 124);
    self.segment.selectedSegmentIndex = 0;
    self.segment.frame = CGRectMake(10,5 + NavBarHeight, ScreenWidth-20, 30);
    [self.segment setContentOffset:CGSizeMake(1,0) forSegmentAtIndex:0];
    [self.segment setContentOffset:CGSizeMake(4,0) forSegmentAtIndex:1];
    [self Change:self.segment];
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:[UIColor blackColor]};
    [self.segment setTitleTextAttributes:dic forState:UIControlStateNormal];
    [self.segment addTarget:self action:@selector(Change:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.segment];
}

-(void)didUploadButttonClicked:(UIButton *)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"是否确定上传?"
                                                    message:nil
                                                   delegate:nil
                                          cancelButtonTitle:@"取消"
                                          otherButtonTitles:@"确定", nil];
    [alert show];
    alert.tag = 200;
    alert.delegate = self;
    
}


- (void)Change:(UISegmentedControl *)seg {
    NSInteger index = seg.selectedSegmentIndex;
    self.isContrler = index;
    switch (index) {
        case 1:{
            [self addChildViewController:self.HDidUploadVC];
            [self.Rootviews addSubview:self.HDidUploadVC.view];
            
        }
            break;
        case 0:{
            [self addChildViewController:self.HunUploadVC];
            [self.Rootviews addSubview:self.HunUploadVC.view];
            
        }
            break;
        default:
            break;
    }
}
- (UIView *)Rootviews
{
    if (!_Rootviews) {
        _Rootviews = [[UIView alloc]initWithFrame:CGRectMake(0, self.segment.bottom+ 15, ScreenWidth, ScreenHeight - self.segment.bottom - 15)];
        _Rootviews.backgroundColor = [UIColor whiteColor];
        _Rootviews.userInteractionEnabled = YES;
    }
    return _Rootviews;
}

- (HNurseAssessUnloadViewController *)HunUploadVC
{
    if (!_HunUploadVC) {
        _HunUploadVC = [[HNurseAssessUnloadViewController alloc]initWithParam:self.theDictMenuParam typeTag:1];
        _HunUploadVC.type = self.type;
        _HunUploadVC.view.height = self.Rootviews.height;
    }
    return _HunUploadVC;
}
- (HNurseAssessUnloadViewController *)HDidUploadVC
{
    if (!_HDidUploadVC) {
        _HDidUploadVC = [[HNurseAssessUnloadViewController alloc]initWithParam:self.theDictMenuParam typeTag:2];
        _HDidUploadVC.type = self.type;
        _HDidUploadVC.view.height = self.Rootviews.height;
    }
    return _HDidUploadVC;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 200) {
        switch (buttonIndex) {
            case 0:{
                return;
            }
                break;
            case 1:{
                if ([self.type isEqualToString:@"manyidu"]) {
                    [HCommon popupMessage:@"正在上传"];
                    InitDbTableHSatisfaction *initd = [[InitDbTableHSatisfaction alloc]init];
                    NSMutableArray *array = [initd getHSatisFaction];
                    
                    NSString *jsonTextDetail = [ModelToJson toJSON:array options:NSJSONWritingPrettyPrinted error:nil];
                    
                    NSLog(@"上传:======%@",jsonTextDetail);
                    
                    
                    UploadPubQuality *upload = [[UploadPubQuality alloc]init];
                    [upload startUpload:jsonTextDetail block:^(NSDictionary *param) {
                        BOOL flag =  [[param valueForKey:@"flag"] boolValue];
                        if( flag ){
                            [HCommon dismissMessage];
                            [HCommon popupSuccessMessage:[NSString stringWithFormat:@"%@",[param objectForKey:@"msg"]]];
                            [self.HunUploadVC updateUploadFlag];
                            NSNotification * notice = [NSNotification notificationWithName:@"UploadSuccess" object:nil userInfo:nil];
                            [[NSNotificationCenter defaultCenter]postNotification:notice];
                            
                        }
                        NSString *msg = (NSString*)[param objectForKey: @"msg"];
                        
                        [self alert:msg];
                    }];

                }else {
                        [HCommon popupMessage:@"正在上传"];
                        InitDbTableQuality *initd = [[InitDbTableQuality alloc]init];
                        NSMutableArray *array = [initd GetDetailIUnUploadTable];
                        NSMutableArray *dataArray = [initd GetMainTableWithUpload_flag:0 typy_id:[self.theDictMenuParam objectForKey:@"TYPE_ID" ]level_id:@"2"];
                    Model_Hr_Ns_Inspect_New *model = dataArray[0];
                    
                    NSMutableArray *Inspect = [initd getInspextNewWithReslut_id:model.RESULT_ID];

                        NSString *jsonText = [ModelToJson toJSON:dataArray options:NSJSONWritingPrettyPrinted error:nil];
                        NSString *jsonTextDetail = [ModelToJson toJSON:array options:NSJSONWritingPrettyPrinted error:nil];
                        NSString *jsonInspect = [ModelToJson toJSON:Inspect options:NSJSONWritingPrettyPrinted error:nil];
                        UploadPubQuality *upload = [[UploadPubQuality alloc]init];
                        
                        [upload startUpload:jsonText inspect_detail:jsonTextDetail  inspectNew: jsonInspect menuid:10006 block:^(NSDictionary *param){
                            
                            BOOL flag =  [[param valueForKey:@"flag"] boolValue];
                            if( flag ){
                                [HCommon dismissMessage];
                                [self.HunUploadVC updateCaozuoUploadFlag];
                                NSNotification * notice = [NSNotification notificationWithName:@"UploadSuccess" object:nil userInfo:nil];
                                [[NSNotificationCenter defaultCenter]postNotification:notice];
                            }
                            NSString *msg = (NSString*)[param objectForKey: @"msg" ];
                            [self alert:msg];
                        }];
                }
            }
                break;
            default:
                break;
        }
    }
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"是否确定删除?"
                                                        message:nil
                                                       delegate:nil
                                              cancelButtonTitle:@"取消"
                                              otherButtonTitles:@"确定", nil];
        [alert show];
        alert.tag = 200;
        alert.delegate = self;
        
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}


@end
